$(document).ready(function(){
/***************************************************************************************************************************************/
/******************************************************REGISTRATION********************************************************************/
    //User
    $(".reg_user").live("click",function(){
        error = 0;message = "";
        $.post("/ajax/saveUser",$(".sign_up").serialize()+'&saveUser=true',function(data){
            if(data=="empty"){
                message = "Please fill those required fields";
                error+=1;
            }else{
                //not empty
                if(data=="error_name"){
                    //false
                    message = "Wrong format of name";
                    error+=1;
                }else{
                    if(data=="error_pass"){
                        message = "Password did not match";
                        error+=1;
                    }else{
                        if(data=="min_max_pass"){
                            message = "Please input password length from 5 to 15";
                            error+=1;
                        }else{
                            if(data=="user_taken"){
                                message = "Your username has already taken, Please choose another";
                                error+=1;
                            }else{
                                // Wrong Captcha code
                                if(data=="Captcha code doesnt match."){
                                    message = "Please type the correct captcha code.";
                                    error+=1;
                                }else{
                                    if(data=="true"){
                                        showNotification({
                                           message: "You are now REGISTERED!! You will redirect to login page",
                                           type: "success",
                                           autoClose: true,
                                           duration: 5
                                        });
                                        $("html").append('<meta http-equiv="refresh" content="3">'); //for refresh
                                    }else
                                        if(data=="invalid code"){
                                            message = "Please enter valid Company Code";
                                            error+=1;
                                        }else{
                                            alert(data)
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if(error>0){
               showNotification({
                  message: message,
                  type: "error",
                  autoClose: true,
                  duration: 10
               });
            }
        })
    });
    
    // Register Company
    $(".reg_company").live("click",function(){
        $.post("/ajax/saveUser",$(".sign_up_company").serialize()+"&saveCompany=true",function(data){
            errorComp = 0;messageComp = 0;
            if(data=="empty fields"){
                errorComp+=1;
                messageComp = "Please Complete all fields";
            }else{
                if(data=="invalid code"){
                    errorComp+=1;
                    messageComp = "Your Company code is Invalid, Try Again!"
                }else{
                    if(data=="Incorrect email format."){
                        errorComp+=1;
                        messageComp = "Please type your correct email format."
                    }else{
                        if(data=="Captcha code doesnt match."){
                            errorComp+=1;
                            messageComp = "Please type the correct captcha code.";
                        }else{
                            if(data=="true"){
                                showNotification({
                                    message: "Registration Succesful!",
                                    type: "success",
                                    autoClose: true,
                                    duration: 3
                                });
                                //reserve for loading
                                $("html").append('<meta http-equiv="refresh" content="2">'); //for refresh
                            }
                        }   
                    }
                }
            }
            if(errorComp>0){
                showNotification({
                    message: messageComp,
                        type: "error",
                   autoClose: true,
                   duration: 3
                });
            }
        })
    });
    
    
});
/***********************************************************Table**********************************************************************/
/******************************************************Home Page (Request)*************************************************************/
/* Set Auto Default to view the list of file from the database */

page = 1;
// Request to Approve
$.post("/ajaxTable/tblRequestToApprove", {action:"RequestApprove",page:page}, function(table){
        $("#requestToApprove_tbl").html(table);
});
// Pending Request
$.post("/ajaxTable/tblPendingAction", {action:"PendingRequest",page:page}, function(table){
        $("#pending_action_tbl").html(table);
});
// My Request
$.post("/ajaxTable/tblMyRequest", {action:"MyRequest",page:page}, function(table){
        $("#myRequest_tbl").html(table);
});



    
// For Searching, Pagination, Sortable
table = {
    /*-------------------------------------------------------------------------------------------------------------------*/
    // Advance Searching
    searchByDateRequestToApprove : function(elements){
        $(elements).live("click", function(){
            if($(this).attr("checked",true)){
                search_action = $(this).attr("data-search-action");
                page = 1;
                $("#search_"+search_action).val(null);
                $("#search_"+search_action+"_alls").val(null);
                $("#search_"+search_action+"_from").val(null);
                $("#search_"+search_action+"_to").val(null);
                $("#search_"+search_action+"_date").show();
                $("#search_"+search_action+"_by").hide();
                $("#search_"+search_action+"_all").hide();
                
                $(".searchAll").attr("checked",false); 
                $(".defaultDa").attr("checked",false);
                
                
                // Request to Approve
                $.post("/ajaxTable/tblRequestToApprove", {action:"RequestApprove",page:page}, function(table){
                        $("#requestToApprove_tbl").html(table);
                });
                
            }
        });  
    },
    searchAllRequestToApprove : function(element){
        $(element).live("click", function(){
            if($(this).attr("checked",true)){
                search_action = $(this).attr("data-search-action");
                page = 1;
                $("#search_"+search_action).val(null);
                $("#search_"+search_action+"_alls").val(null);
                $("#search_"+search_action+"_from").val(null);
                $("#search_"+search_action+"_to").val(null);
                $("#search_"+search_action+"_date").hide();
                $("#search_"+search_action+"_by").hide();
                $("#search_"+search_action+"_all").show();
                
                $(".searchByDate").attr("checked",false); 
                $(".defaultDa").attr("checked",false);
                // Request to Approve
                $.post("/ajaxTable/tblRequestToApprove", {action:"RequestApprove",page:page}, function(table){
                        $("#requestToApprove_tbl").html(table);
                });
            }    
        });
    },
    defaultDaRequestToApprove : function(elements){
        $(elements).live("click", function(){
            if($(this).attr("checked",true)){
                search_action = $(this).attr("data-search-action");
                data_location = $(this).attr("data-location");
                data_table = $(this).attr("data-table");
                page = 1;
                $("#search_"+search_action).val(null);
                $("#search_"+search_action+"_alls").val(null);
                $("#search_"+search_action+"_from").val(null);
                $("#search_"+search_action+"_to").val(null);
                $("#search_"+search_action+"_date").hide();
                $("#search_"+search_action+"_by").show();
                $("#search_"+search_action+"_all").hide();
                
                $(".searchAll").attr("checked",false); 
                $(".searchByDate").attr("checked",false);
                
                // Request to Approve
                $.post("/ajaxTable/"+data_location, {action:"RequestApprove",page:page}, function(table){
                        $("#"+data_table).html(table);
                });
            }      
        });
    },
    
    
    //for Searching Default
    search_requestApprove : function(elements){
        $(elements).live("keyup",function(){
            data_location_defaulit = $(this).attr("data-location-default");
            data_location = $(this).attr("data-location");
            data_table = $(this).attr("data-table");
            search_action = $(this).attr("data-search-action");
            value = $(this).val();
            page = 1;
            if(value!=""){
                // Request to Approve
                $.post("/ajaxTable/"+data_location, {action:"search_requestApprove",page:page,value:value}, function(table){
                        $("#"+data_table).html(table);
                });
            }else{
                // Request to Approve
                $.post("/ajaxTable/"+data_location, {action:data_location_defaulit,page:page}, function(table){
                        $("#"+data_table).html(table);
                });
            }
                /*
                    
                */
                
        });
    },
    // For Searching ALL
    search_requestApprove_alls : function(elements){
        $(elements).live("keyup", function(){
            searchInput = $(this).val();
            data_location = $(this).attr("data-location");
            data_table = $(this).attr("data-table");
            $("tr."+data_table).each(function(){
                var re = new RegExp(searchInput, 'gi');
                if($(this).text().match(re) === null )
                {
                    $(this).hide();
                    $(".pagination").hide();
                }else{
                    $(this).show();
                    $(".pagination").show();
                }
            });
        });
    },
    // For Date Search
    searchDate : function(elements){
        $(elements).live("click", function(){
            data_location = $(this).attr("data-location");
            data_table = $(this).attr("data-table");
            search_action = $(this).attr("data-search-action");
            dateF = $("#search_"+search_action+"_from").val();
            dateT = $("#search_"+search_action+"_to").val();
            value = dateF + "," + dateT;
            action = "searchDate";
            page = 1;
        // Request to Approve
            $.post("/ajaxTable/"+data_location, {action:action,page:page,value:value}, function(table){
                $("#"+data_table).html(table);
            });
        });
    },
    /*-------------------------------------------------------------------------------------------------------------------*/
    //for Sorting
    sortRequestApprove : function(element){
        counterSort = 0;
        $("tr"+element+" >th").live("click", function(){
            if($(this).attr("rel")!="notAllowed"){
                // Get value of the selected header in the table
                //dataSortValue = $(this).closest('table').find('th').eq($(event.target).index()).attr("id");
                page = $(".requestToApprove_page").attr("p");
                dataSortValue = $(this).parent().attr("data-search");
                action = $(this).parent().attr("data-tag-action");
                elementsShow = $(this).parent().attr("data-element");
                data_location = $(this).parent().attr("data-location");
                // Request to Approve
                    tblRequestToApprove(action,page,elementsShow,dataSortValue,counterSort,data_location);
                counterSort++;    
            }
        });
    },
    /*-------------------------------------------------------------------------------------------------------------------*/
    //for pagination
    tablePagination : function(elements){
        $(elements).live('click',function(){
            var page = $(this).attr('p');
            action = $(this).attr('data-tag-action');
            value = $(this).attr("data-value");
            
                // Request to Approve
                $("#requestToApprove").attr("p",page);
                $.post("/ajaxTable/tblRequestToApprove", {action:action,page:page,value:value}, function(table){
                        $("#requestToApprove_tbl").html(table);
                });
                
                // Pending Request    
                $.post("/ajaxTable/tblPendingAction", {action:action,page:page,value:value}, function(table){
                        $("#pending_action_tbl").html(table);
                });
                
                // My Request    
                $.post("/ajaxTable/tblMyRequest", {action:action,page:page,value:value}, function(table){
                        $("#myRequest_tbl").html(table);
                });
                        
            
        });
        
    }
}
$(document).ready(function(){
    // Tbl Plugin (Search, table plug, sorting)
    // Advance Searching
    table.searchByDateRequestToApprove(".searchByDate"); // Enabling Searching for date
    table.searchAllRequestToApprove(".searchAll"); // Enabling Searching for all
    table.defaultDaRequestToApprove(".defaultDa"); // Enabling Searching for Default 
    
    // Searching
    table.search_requestApprove(".searchRA"); // Search Request To Approve
    table.search_requestApprove_alls(".searchRAall"); // Search All
    table.searchDate("#searchDateRequestToApprove"); // Search Date
    
    // Sorting
    table.sortRequestApprove("#sortRequestApprove"); // Sorting Request To Approve Table
    table.sortRequestApprove("#sortPendingAction"); // Sorting Request To Approve Table
    table.sortRequestApprove("#sortMyRequest"); // Sorting Request To Approve Table
    // Pagination
    table.tablePagination('.pagination li.active'); // For pagination
    
    $(".dateFormatSearch").datepicker({ dateFormat: 'yy-mm-dd' });
});

//---------------------------------------------------------------------------------------------//
/* Create Functions */
function tblRequestToApprove(action,page,elementsShow,dataSortValue,counterSort,data_location){
    $.post("/ajaxTable/"+data_location, {action:action,page:page,dataSortValue:dataSortValue,counterSort:counterSort}, function(table){
        $(elementsShow).html(table);
    });
}
